home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / zmdm / fileio.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  2KB  |  128 lines

  1. /*
  2.  *     File I/O (with large buffers) Module
  3.  *
  4.  *        Jwahar Bammi
  5.  *            usenet: cwruecmp!bammi@decvax.UUCP
  6.  *            csnet:  bammi@cwru.edu
  7.  *            arpa:   bammi@cwru.edu
  8.  *            CompuServe: 71515,155
  9.  */
  10.  
  11. #include "zmdm.h"
  12. #include "common.h"
  13.  
  14. #define    O_RDONLY    1
  15. #define O_WRONLY    2
  16. #define O_APONLY    4
  17. #define MBUFSIZ        (((long)BBUFSIZ)-1L)
  18.  
  19. static unsigned char *bptr;
  20.  
  21. static long flcount = (-1L);
  22. static char bufmode;
  23.  
  24. int stfopen(name, mode)
  25. char *name, *mode;
  26. {
  27.     register int handle;
  28.  
  29.     switch(*mode)
  30.     {
  31.         case 'r':
  32.         if((handle = Fopen(name, 0)) <= 0)
  33.             return -1;
  34.         bufmode = O_RDONLY;
  35.         break;
  36.  
  37.         case 'w':
  38.         if((handle = Fcreate(name, 0)) <= 0)
  39.         {
  40.             if((handle = Fopen(name, 1)) <= 0)
  41.                 return -1;
  42.         }
  43.         bufmode = O_WRONLY;
  44.         break;
  45.  
  46.         case 'a':
  47.         if((handle = Fopen(name, 2)) <= 0)
  48.             return -1;
  49.         Fseek(0L, handle, 2);
  50.         bufmode = O_APONLY;
  51.         break;
  52.  
  53.        default:
  54.         return -1;
  55.     }
  56.     bptr = bufr;
  57.     flcount = (-1L);
  58.     return handle;
  59. }
  60.  
  61. stfclose(handle)
  62. int handle;
  63. {
  64.     if(bufmode == O_RDONLY)
  65.         return Fclose(handle);
  66.     if(stflush(handle))
  67.     {
  68.         Fclose(handle);
  69.         return -1;
  70.     }
  71.     return Fclose(handle);
  72. }
  73.  
  74. stputc(c, handle)
  75. unsigned int c;
  76. int handle;
  77. {
  78.     if(flcount >= MBUFSIZ)
  79.     {
  80.         if(Fwrite(handle, (long)BBUFSIZ, bufr) != (long)BBUFSIZ)
  81.             return -1;
  82.         flcount = (-1L);
  83.         bptr  = bufr;
  84.     }
  85.     flcount++;
  86.     return (*bptr++ = c);
  87. }
  88.  
  89. stgetc(handle)
  90. int handle;
  91. {
  92.     if(flcount <= 0)
  93.     {
  94.         if((flcount = Fread(handle, (long)BBUFSIZ, bufr)) == 0)
  95.             return EOF;
  96.         bptr = bufr;
  97.     }
  98.     flcount--;
  99.     return(*bptr++);
  100. }
  101.  
  102. stflush(handle)
  103. int handle;
  104. {
  105.     if(flcount < 0)
  106.         return 0;
  107.  
  108.     if(Fwrite(handle, (long)(flcount+1L), bufr) != (flcount+1L))
  109.         return -1;
  110.     flcount = (-1L);
  111.     bptr = bufr;
  112.     return 0;
  113. }
  114.         
  115. stfseek(handle, disp, mode)
  116. int handle;
  117. long disp;
  118. int  mode;
  119. {
  120.     if(bufmode != O_RDONLY)
  121.         if(stflush(handle))
  122.             return -1;
  123.     Fseek(disp, handle, mode);
  124.     flcount = (-1L);
  125.     bptr = bufr;
  126.     return 0;
  127. }
  128.